#!/bin/bash
#Written by Matthew J Vona 6/19/2014
#REPORTER IS THE SINGLE TSR FOR CARBLOGGER EXCEPT FOR THE DRIVERS.
#THIS HAS BEEN RE-WORKED TO MORE EASILY ALLOW SCHEDULING TASKS, AS WELL AS
#TO SEND DIAGNOSTIC STATUS EMAILS BACK TO STATION OPERATORS.
trap cleanup 5 6 7 8 9
#CLEANUP TASKS TO WRITE
cleanup()
{
	echo "Cleaning up.."
	echo "Done"
	exit;
}
##########################################################
#READ THE CONFIGUARTION FILE
##########################################################
while read line; do
	eval $line
done < Config/config
 


##########################################################
# DEFINE A FUNCTION TO RESYNC PROCESSES TO THE TOP OF THE 
# NEXT MINUTE
#########################################################
wait_next(){
	while [ `date '+%S'` -ne 00 ]; 	do
		sleep .1
	done
}

#########################################################
#	BEGIN EMAIL SENDING ROUTINE                     #
#########################################################
# CLEAN ERRORS IS CALLED BY THE EMAIL WRITING ROUTINE	#
# THE PURPOSE IS TO REMOVE ERRORS RECORDED AFTER BEING	#
# SENT TO THE SITE OPERATOR                             #
#########################################################
clean_errors()
{
	for (( i = 0 ; i < 10 ; i++ )); do
		echo "" > Data/errors0$i
	done
	for (( i = 10 ; i < 25 ; i++ )); do
		echo "" > Data/errors$i
	done
}

#############################################################
#
#Truncate mysql database Function
#
#############################################################
truncate()
{
	mysql -u root --password=carblogger carblogger -e "DELETE from cl_ingests where SAMP_TIME<(NOW() - INTERVAL 2 MONTH)";
}


#############################################################
#DMSOUT FUNCTION                                            #
#                                                           #
#############################################################
dmsout()
{
	bash sys_resources/dmsout.sh "$outbegin" "$outend" 1> /dev/null 2> /dev/null 
	for i in `ls -t *.MIN`; do
		bash sys_resources/carbput.sh $i > /dev/null 2>&1
	done
}
###########################################################
# SPMOUT FUNCTION                                         #
#edit 8/15/2014 MQ                                        #
#added raw data e-mail out                                #
###########################################################
spmout () 
{
	compressed_name=`date -d "yesterday" "+raw%d%m%Y.tgz"`
	compressed_name="${saroad}_backup_${compressed_name}"
	tar czf "$compressed_name" Data/`date -d "yesterday" "+${saroad}-%Y-%d-%m"`
	bash sys_resources/spmout.sh $yesterday $yesterday_month `date -d yesterday "+%Y"` 1> /dev/null 2> /dev/null
	recips="$siteop $sitesup"
	if [ $scnd_email == "y" ]; then
		recips="$recips $sitescnd"
	fi
	echo "Raw Data File" | timeout 120 mutt -s "$sitnam Raw data File" -a ${compressed_name} -- ${recips} && sleep 30
  
  #send all raw backup files to server for backup
  temp=`ls *.tgz`
  for i in $temp; do
    bash sys_resources/carbput.sh $i
	done   
    
	#bash sys_resources/carbput.sh "$compressed_name" > /dev/null 2>&1
}

############################################################
#	STATUS E-MAIL FUNCTION                             #
# Sends e-mails on report hour                             #
#broken out of main code by MQ 6/27/2014                   #
############################################################
email_status ()
{
	if [ $hour == $hour1 ] || [ $hour == $hour2 ] ; then
		#REMOVE THE OLD ERRORS FILE IF IT EXISTS
		rm -f sys_resources/.tmpcom &>/dev/null
		#INSERT A BLANK LINE IN THE BEGINING
		echo "" >> sys_resources/.tmpcom
		#START ADDING TO THE BODY OF THE EMAIL
		echo "Site: $sitnam CARBLogger 	Reporting for `date "+%m"`/`date "+%d"`" >> sys_resources/.tmpcom
		echo "Site Operator:${siteop} Site Supervisor:${sitesup} " >> sys_resources/.tmpcom
		number=$(ps -fe | grep testd | grep -v grep | wc -l)
		nmbrinsts=$(echo $active_inst | wc -w)
		echo "Number of Active Instrument Processes: $number                               " >> sys_resources/.tmpcom
		echo "Number of Active Instruments Configured: $nmbrinsts                               " >> sys_resources/.tmpcom
		echo "Drivers Currently Utilized: $active_inst" >> sys_resources/.tmpcom
		echo "DATA STATE - - - - - - - - - - - - - - - - - - " >> sys_resources/.tmpcom
		size=$(ls -lah Data | grep total)
		al_cons=$(cat Data/errors[0-2][0-9] 2>/dev/null | grep : | wc -l)
		echo "Data Directory Size: ${size} 	Alerts Recorded: $al_cons                        #" >> sys_resources/.tmpcom
		echo " -----------------------ALERT DETAILS- - - - - - - - - - - - - - - - - - - " >> sys_resources/.tmpcom
		count_drv=1
		for i in ${active_inst}; do
			drv_list[$count_drv]="$i"
			usr_count=$(ls -B resources/$i/.usr*)
			for k in $usr_count; do
				temp=$(echo "${k#*/*/.usr}")
				chn_nam[$count_drv]=$(cat resources/$i/.usr$temp | grep -o "\-r [-A-Za-z0-9_]*" | awk '{ print $2 }')
				count_drv=`expr $count_drv + 1`
				max_count_drv="$count_drv"
			done
		done
		#TEST TO SEE IF ANY ERRORS FILES EXIST; IF SO PREPARE THEM
		if (stat Data/errors* 1> /dev/null 2>/dev/null); then
			#read all errors files, sor the contents to count the numbers for each
			cat Data/errors[0-2]* | grep -v ^$ | sort >Data/.tmpstats
			#create an errors.txt file from the sorted contents
			cp Data/.tmpstats Data/errors.txt
			#for each instrument tally the number of errors present
			#and write in the .tmpcom file in the sys_resources folder
			for (( i = 1 ; i < max_count_drv ; i++ )); do
				num_err=$(cat Data/.tmpstats | grep "${chn_nam[$i]} "| wc -l)
				echo "${chn_nam[$i]} has $num_err errors" >> sys_resources/.tmpcom
			done
		 fi
		#SET THE EMAIL SUBJECT BASED ON THE NUMBER OF ERRORS
		subject="CARBLogger: Status"
		if [ $al_cons -gt 0 ]; then
			subject=$subject" $sitnam Includes Errors"
		else
			subject=$subject" $sitnam Nominal"	
		fi
		recips="$siteop $sitesup";
		if [ $scnd_email == "y" ]; then
			recips="$recips $sitescnd";
		fi
		listcfgs=''
		for i in `ls -lah resources/*BAM1020*_drv/.bamcfg* 2> /dev/null | awk '{ print $9 }'`; do
			listcfgs="-a $i ${listcfgs}"
		done

		cat "sys_resources/.tmpcom" | timeout 120 mutt -s "$subject" -a "Data/errors.txt" $listcfgs -- $recips && sleep 10
		rm "Data/errors.txt"
		rm resources/*BAM1020*_drv/.bamcfg* 2>/dev/null
		clean_errors
		#~~~~~~~~~~~~~~~~~~~~~~~~~ATTACH & SEND CSV EXPORT~~~~~~~~~~~~~~~~~#
		  
		if (stat text_summary_[0-3][0-9]* 1>/dev/null 2>/dev/null); then
			echo "Data Extracts" | timeout 120 mutt -s "$sitnam data" -a text_summary_[0-3][0-9]* -- $recips && sleep 30
			rm -f text_summary_[0-3][0-9]* 1> /dev/null 2> /dev/null
		fi
	fi
}


#########################################################
#BEGIN THE MAIN LOOP                                    #
#THIS LOOP CONTINUES TO RUN 24x7. PLACE TASK            #
#YOU WANT TO RUN INTO A MINUTE BIN IN THE CASE          #
#STATEMENT BELOW.                                       #
#########################################################
while :
do
 	#############################
	#LOOP CONTROL/TIME VARIABLES#
	#############################
	#CURRENT TIME
	hour=`date "+%H"`
	minute=`date "+%M"`
	month=`date "+%m"`
	day=`date "+%d"`
	year=`date "+%Y"`
	#YESTERDAY
	yesterday=`date -d yesterday "+%d"`
	yesterday_month=`date -d yesterday "+%m"`
	ingested="0"
	reported="0"
	tar_flag="0"
	#10 MINUTES AGO
	ingest_hour=`date -d "59 minutes ago" '+%H'`
	ingest_day=`date -d "59 minutes ago" '+%d'`
	ingest_month=`date -d "59 minutes ago" '+%m'`
	ingest_year=`date -d "59 minutes ago" '+%Y'`  	
	######################################################################
	#	BEGIN HOURLY CASE STATEMENT:                                 #	
	#	USE THIS STATEMENT TO SCHEDULE EVENT IN MUCH THE SAME WAY AS #
	#	WITH CRON.                                                   #
	#	HERE ARE SOME EXAMPLES:                                      #
	#	TO ADD A NEW ACTION STATEMENT AT MINUTE 20:                  #
	#	20)                                                          #
	#		<OLD STATEMENT>                                      #
	#		<STATEMENT>                                          #
	#		wait_next                                            #
	#	;;                                                           #
	######################################################################
	case $minute in
		00)
			/bin/bash sys_resources/ingest.sh $ingest_hour $ingest_day $ingest_month $ingest_year '[4-5]' 1> /dev/null 2> /dev/null 
			sleep 1
			rm -f .[0-9]* 2>/dev/null
			wait_next
			;;
		10)
			/bin/bash sys_resources/ingest.sh $ingest_hour $ingest_day $ingest_month $ingest_year 5 1> /dev/null 2> /dev/null
			/bin/bash sys_resources/ingest.sh $hour $day $month $year 0 1> /dev/null 2> /dev/null
			outbegin=`date -d "1 hours ago" '+%Y-%m-%d %H:00:00'`
			outend=`date '+%Y-%m-%d %H:00:00'`
			dmsout &
			wait_next
			;;
		20)
			/bin/bash sys_resources/ingest.sh $hour $day $month $year '[0-1]' 1> /dev/null 2> /dev/null
			#HERE WE INGEST THE PREVIOUS HOUR 
			/bin/bash sys_resources/ingest.sh $ingest_hour $ingest_day $ingest_month $ingest_year 1> /dev/null 2> /dev/null
			wait_next
			;;
		30)
			/bin/bash sys_resources/ingest.sh $hour $day $month $year '[1-2]' 1> /dev/null 2> /dev/null
			/bin/bash sys_resources/backpoll.sh &> /dev/null &
			outbegin=`date -d "2 hours ago" '+%Y-%m-%d %H:00:00'`
			outend=`date -d "1 hours ago" '+%Y-%m-%d %H:00:00'`
			bash sys_resources/dmsout.sh "$outbegin" "$outend" 1> /dev/null 2> /dev/null &
			wait_next
			;;
		
		40)
			/bin/bash sys_resources/ingest.sh $hour $day $month $year '[2-3]' 1> /dev/null 2> /dev/null
			
			if [ "$hour" -eq "03" ]; then
				spmout &
			fi
			wait_next
			;;
		50)
			/bin/bash sys_resources/ingest.sh $hour $day $month $year '[3-4]' 1> /dev/null 2> /dev/null
			email_status &		
			if [ "$hour" -eq "01" ]; then
				truncate &
			fi
			wait_next
			;;
		*)
			wait_next			  
			;;
	esac

done;
